package com.juyu.miliao.common.util;

import java.math.BigDecimal;
import java.util.Random;

public class DoubleFormatUtil {
	/**
	 * 保留小数两位
	 * 
	 * @return double
	 */
	public static double doubleFormate(double num) {
		BigDecimal bg = new BigDecimal(num);
		num = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return num;
	}

	/**
	 * double 转int
	 * @param number
	 * @return
	 */
	public static int intFormate(double number) {
		BigDecimal bd = new BigDecimal(number).setScale(0, BigDecimal.ROUND_HALF_UP);
		return Integer.parseInt(bd.toString());
	}

	/**
	 * Double随机数
	 * @param maxNum 最大随机值
	 * @return double随机数结果保留两位小数
	 */
	public static double randomDouble(double maxNum){
		double boundedDouble = 0.01 + new Random().nextDouble() * (maxNum - 0.01);
		return doubleFormate(boundedDouble);
	}
}
